﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using IPTV.DBUtility;
using IPTV.module;
using System.Data;

/// <summary>
/// Summary description for PlateDao
/// </summary>
namespace IPTV.dao
{
    public class ZtDao
    {
        public ZtDao()
        {
            
        }

        public List<itv_zhzt_info> getZtTemplateById(string id)
        {

            string sql = " select i.id,i.templateid,i.picture,t.url from itv_zhzt_info i ,"
            + " itv_zhzt_template t where i.templateid = t.id and i.id = @id";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@id" , MySqlDbType.VarChar, 36 )
                };

            parms[0].Value = id;
            List<itv_zhzt_info> ztList = new List<itv_zhzt_info>();
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    itv_zhzt_info zt = new itv_zhzt_info();
                    zt.Id = reader[0].ToString();
                    zt.Templateid = reader[1].ToString();
                    zt.Picture = reader[2].ToString();
                    zt.Url = reader[3].ToString();
                    ztList.Add(zt);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return ztList;

        }


        public List<itv_zhzt_list> getZtById(string id)
        {

            string sql = " select id , sourceid, url, displayorder ,title from itv_zhzt_list where zhztinfoid = @id ORDER BY displayorder asc ";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@id" , MySqlDbType.VarChar, 36 )
                };

            parms[0].Value = id;
            List<itv_zhzt_list> ztList = new List<itv_zhzt_list>();
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    itv_zhzt_list zt = new itv_zhzt_list();
                    zt.Id = reader[0].ToString();
                    zt.Sourceid = reader[1].ToString();
                    zt.Url = reader[2].ToString();
                    zt.Displayorder = reader[3].ToString();
                    zt.Title = reader[4].ToString();
                    ztList.Add(zt);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return ztList;

        }

        public List<CourseDto> findCourseById(string id)
        {

            String sql = " select cve.video_id,cve.video_name,t.name,cve.video_length,cve.remark , "
                        + " cve.tagIDs , cve.keyword  "
                        + " from cms_video_ext cve,teacher t "
                        + " where cve.teacher_id = t.id and cve.video_id = @id";


            List<CourseDto> courseList = new List<CourseDto>();
            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@id" , MySqlDbType.VarChar, 50 )
                };

            parms[0].Value = id;

            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    CourseDto course = new CourseDto();
                    course.VideoId = reader[0].ToString();
                    course.VideoName = reader[1].ToString();
                    course.TeacherName = reader[2].ToString();
                    course.VideoLength = reader[3].ToString();
                    course.Remark = reader[4].ToString();
                    course.TagIDs = reader[5].ToString();
                    course.Keyword = reader[6].ToString();
                    course.Tags=getTagById(course.TagIDs);
                    courseList.Add(course);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("findCourseById run error.");
            }

            return courseList;
        }

       public string  getTagById(string tagIds)
        {
            String[] tempIds = tagIds.Split(';');
            string tempPara = "";
            List<String> paraList = new List<string>();
            for (int i = 0; i < tempIds.Length; i++ )
            {
                string tmp = tempIds[i];
                if (!tmp.Equals(""))
                {
                    String[] tmp2 = tmp.Split('$');

                    for (int j = 0; j < tmp2.Length; j++)
                    {
                        if (!"".Equals(tmp2[j].Trim()))
                        {
                            paraList.Add(tmp2[j]);

                        }
                    }
                }

            }

            for(int i =0;i<paraList.Count ; i++)
            {
                tempPara += "'" + paraList[i] + "'";
                if(i!= paraList.Count  -1)
                {
                    tempPara+=",";
                }

            }

            DictionaryDao dictionaryDao = new DictionaryDao();
           return dictionaryDao.findTags(tempPara);
        }
    
    }
}